﻿Option Explicit On

Public Class FRMexport


    Private sFilePath As String

    Dim i As Integer
    Dim j As Integer

    ' Form load event ----------------------------------------------------------------------------------------------------------------------

    Private Sub Form_Load()

        initForm()

    End Sub

    ' Initialize the form. Set the load the fields list, with the fields in the main form's listbox -------------------------------------

    Public Sub initForm()

        On Error Resume Next

        Dim aSplitDefaultExportFields As Object
        Dim sDefaultExportFieldsList As String

        LISTfields.Items.Clear()

        For i = 0 To FRMmain.ListFields.Items.Count - 1

            LISTfields.Items.Add(FRMmain.ListFields.Items(i))

        Next

        ' Select the default export fields -----------------------------------------------------------------------

        sDefaultExportFieldsList = getPref("", "defaultExportFields")

        aSplitDefaultExportFields = Split(sDefaultExportFieldsList, ",")

        For i = 0 To Me.LISTfields.Items.Count

            For j = 0 To UBound(aSplitDefaultExportFields)

                If aSplitDefaultExportFields(j) = LISTfields.Items(i) Then

                    LISTfields.SetSelected(i, True)

                    Exit For

                End If

            Next

        Next

        TXTexportPath.Text = getDefaultExportFilePath()

        ProgressBar.Visible = False

        CMDExport.Enabled = True

    End Sub

    Private Function export() As Boolean

        Dim bExported As Boolean

        ' Validate file name and field selection, and call the export method in the export module ------------------------------------------

        ' Check that at least one field is selected in the listbox ------------------------------------------------------------------------------

        If LISTfields.SelectedIndices.Count < 1 Then

            MsgBox("Please Choose at Least One Field To Export")

            Exit Function

        End If

        ' Check for a file name ------------------------------------------------------------------------------------------

        If TXTexportPath.Text = "" Then

            MsgBox("Please Type A Name For The File")
            TXTexportPath.Focus()

            Exit Function

        End If

        Dim sFieldsToExport As String

        If Trim(LISTfields.Items(LISTfields.SelectedIndex)) = "All" Then

            sFieldsToExport = "!All!"

        Else

            For i = 0 To LISTfields.SelectedIndices.Count - 1

                If LISTfields.SelectedIndices.Contains(i) = True Then

                    sFieldsToExport = sFieldsToExport & "[" & LISTfields.Items(i) & "],"

                End If

            Next

            ' Remove the last 'comma' --------------------------------------------------------------------------------------

            sFieldsToExport = Utils.Left(sFieldsToExport, Len(sFieldsToExport) - 1)

        End If

        If RADIOexportToExcel.Checked = True Then

            ' Export to Excel file ----------------------------------------------------------------------------------------------------

            bExported = MODexport.exportToExcel(FRMmain.LISTtables.Items(FRMmain.LISTtables.SelectedIndex), sFieldsToExport, TXTexportPath.Text)

        Else

            ' Export to text file -------------------------------------------------------------------------------------------------------

            bExported = MODexport.exportToText(FRMmain.LISTtables.Items(FRMmain.LISTtables.SelectedIndex), sFieldsToExport, TXTexportPath.Text)

        End If

        export = bExported

    End Function


    ' Build a path for the file to be exported -------------------------------------------------------------------------------------------------------

    Private Function getDefaultExportFilePath()
        Throw New NotImplementedException
        'On Error Resume Next

        'Dim _
        '    sFileName As String

        'sFileName = FRMmain.LISTtables.Items(FRMmain.LISTtables.SelectedIndex)

        'sFilePath = Application.ExecutablePath & "\" & sFileName & "_" & Day(Now) & "_" & Month(Now) & ".txt"

        'getDefaultExportFilePath = sFilePath

    End Function

    ' When user clicks browse to choose a path for exporting -----------------------------------------------------------------------------------

    Private Sub CMDbrowse_Click()
        Throw New NotImplementedException
        '        On Error GoTo errorHandler

        '        With FRMmain.CommonDialog

        '            .CancelError = True

        '            .FileName = getDefaultExportFilePath()

        '            .Filter = "Text Documents (*.txt)|*.txt|Excel (*.xls)|*.xls|All Files|*.*"

        '            .ShowSave()

        '            TXTexportPath.Text = .FileName

        '        End With

        '        Exit Sub

        'errorHandler:

        '        Select Case Err.Number

        '            Case 32755 ' Cancel was selected

        '                Exit Sub

        '            Case 20477 ' Invalid file name

        '                MsgBox("The path """ & sFilePath & """ is invalid")

        '                Exit Sub

        '        End Select

    End Sub

    ' Select all items when user clicks 'All' -------------------------------------------------------------------------------------------------

    Private Sub LISTfields_Click()

        On Error Resume Next

        If LISTfields.SelectedIndices.Contains(0) = True Then

            For i = 1 To LISTfields.SelectedIndices.Count - 1

                LISTfields.SetSelected(i, True)

            Next

        End If

    End Sub

    ' Export the database ---------------------------------------------------------------------------------------------------

    Private Sub CMDExport_Click()

        On Error Resume Next

        If export() Then Me.Hide()

    End Sub

    Private Sub RADIOexportToText_Click()

        On Error Resume Next

        If LCase(Utils.Right(TXTexportPath.Text, 4)) = ".xls" Then

            TXTexportPath.Text = Utils.Left(TXTexportPath.Text, Len(TXTexportPath.Text) - 4) & ".txt"

        Else

            TXTexportPath.Text = TXTexportPath.Text & ".txt"

        End If

    End Sub
    Private Sub RADIOexportToExcel_Click()

        On Error Resume Next

        If LCase(Utils.Right(TXTexportPath.Text, 4)) = ".txt" Then

            TXTexportPath.Text = Utils.Left(TXTexportPath.Text, Len(TXTexportPath.Text) - 4) & ".xls"

        Else

            TXTexportPath.Text = TXTexportPath.Text & ".xls"

        End If

    End Sub

    Private Sub CMDcancel_Click()

        Close()

    End Sub

End Class